/**
 * 报表数据获取的组合式函数
 * 可以在任何组件中复用
 */

import { ref } from 'vue';
import { reportApi, schoolApi } from '@/api';

export function useReport() {
  const loading = ref(false);
  const error = ref(null);
  const reportList = ref([]);
  const schoolList = ref([]);

  /**
   * 获取报表列表
   * @param {Object} params - 查询参数
   */
  const fetchReportList = async (params = {}) => {
    loading.value = true;
    error.value = null;
    
    try {
      const res = await reportApi.getReportList(params);
      reportList.value = res.data || [];
      return res.data;
    } catch (err) {
      error.value = err.message || '获取报表列表失败';
      console.error('获取报表列表失败：', err);
      return [];
    } finally {
      loading.value = false;
    }
  };

  /**
   * 获取学校列表
   * @param {Object} params - 查询参数
   */
  const fetchSchoolList = async (params = {}) => {
    loading.value = true;
    error.value = null;
    
    try {
      const res = await schoolApi.getSchoolList(params);
      schoolList.value = res.data || [];
      return res.data;
    } catch (err) {
      error.value = err.message || '获取学校列表失败';
      console.error('获取学校列表失败：', err);
      return [];
    } finally {
      loading.value = false;
    }
  };

  /**
   * 创建报表
   * @param {Object} data - 报表数据
   */
  const createReport = async (data) => {
    loading.value = true;
    error.value = null;
    
    try {
      const res = await reportApi.createReport(data);
      return res;
    } catch (err) {
      error.value = err.message || '创建报表失败';
      console.error('创建报表失败：', err);
      throw err;
    } finally {
      loading.value = false;
    }
  };

  /**
   * 更新报表
   * @param {Number} id - 报表ID
   * @param {Object} data - 报表数据
   */
  const updateReport = async (id, data) => {
    loading.value = true;
    error.value = null;
    
    try {
      const res = await reportApi.updateReport(id, data);
      return res;
    } catch (err) {
      error.value = err.message || '更新报表失败';
      console.error('更新报表失败：', err);
      throw err;
    } finally {
      loading.value = false;
    }
  };

  /**
   * 删除报表
   * @param {Number} id - 报表ID
   */
  const deleteReport = async (id) => {
    loading.value = true;
    error.value = null;
    
    try {
      const res = await reportApi.deleteReport(id);
      return res;
    } catch (err) {
      error.value = err.message || '删除报表失败';
      console.error('删除报表失败：', err);
      throw err;
    } finally {
      loading.value = false;
    }
  };

  /**
   * 导出报表
   * @param {Object} params - 导出参数
   */
  const exportReport = async (params = {}) => {
    loading.value = true;
    error.value = null;
    
    try {
      const res = await reportApi.exportReport(params);
      
      // 处理文件下载
      const blob = new Blob([res], { 
        type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' 
      });
      const url = window.URL.createObjectURL(blob);
      const link = document.createElement('a');
      link.href = url;
      link.download = `报表_${new Date().getTime()}.xlsx`;
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      window.URL.revokeObjectURL(url);
      
      return true;
    } catch (err) {
      error.value = err.message || '导出报表失败';
      console.error('导出报表失败：', err);
      return false;
    } finally {
      loading.value = false;
    }
  };

  return {
    loading,
    error,
    reportList,
    schoolList,
    fetchReportList,
    fetchSchoolList,
    createReport,
    updateReport,
    deleteReport,
    exportReport
  };
}

